{extend name="admin@index_layout"/}
{block name="main"}
<link rel="stylesheet" href="__STATIC__/admin/css/cms.css">
<div class="layui-card">
    <div class="layui-card-header">源语言站：{$masterName} 目标语言站：{$siteName}</div>
    <div class="layui-card-body">
        <div class="layui-form">
            <table class="layui-hide" id="currentTable" lay-filter="currentTable"></table>
            <blockquote class="layui-elem-quote">提示：该操作将覆盖目标语种相关信息，若目标语种相关信息已修改，请谨慎操作！</blockquote>
        </div>
        <script type="text/html" id="toolbar">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-sm yzn-btn-primary" lay-event="refresh"><i class="iconfont icon-shuaxin1"></i> </button>
            </div>
        </script>
        <input type="hidden" name="id" value="{$id}">
    </div>
</div>
<div id="change_div" style="width:400px;height:250px;display: none;padding: 20px;background: #f2f2f2;">
    <div class="scroller" style="height:250px" id="change_html"></div>
</div>
<script type="text/html" id="sites">
    {{# if(d.sites.indexOf({$id})!==-1){ }}
    <span class="text-success">已推送</span>
    {{#  } else { }}
    <span class="text-danger">未推送</span>
    {{#  } }}
</script>

<script type="text/html" id="barTool">
    {{# if(d.sites.indexOf({$id})!==-1){ }}
    <a class="layui-btn layui-btn-xs" lay-event="push" title="重新推送">重新推送</a>
    {{#  } else { }}
    <a class="layui-btn layui-btn-xs layui-btn-warm" lay-event="push" title="翻译推送">翻译推送</a>
    {{#  } }}
</script>
{/block}
{block name="script"}
<script>
    layui.use(['table'], function() {
        var table = layui.table;
        var site_id = $("input[name='id']").val();
        table.render({
            id: 'currentTable',
            elem: '#currentTable',
            toolbar: '#toolbar',
            url: '{:url("site")}',
            search:false,
            cols: [
                [
                    { field: 'id', width: 70, title: 'ID',search:false , align: "center"},
                    { field: 'name',align: "left", title: '类别' },
                    { field: 'sites', width: 100, title: '是否推送', templet:"#sites", align: "center" },
                    { field: 'updatetime', width: 145, title: '同步时间', search: 'range',hide: true },
                    {fixed: 'right', title:'操作',width:220, toolbar: '#barTool',align:"center"}
                ]
            ],
        });


        //监听工具条
        table.on('tool(currentTable)', function(obj){
            var data = obj.data,that = $(this);
            if(obj.event === 'push'){
                //类别推送
                layer.confirm('操作将覆盖原有推送信息，是否继续操作？', function(index){
                    that.addClass('layui-btn-disabled').prop('disabled', true).html('<i class="layui-icon layui-icon-loading layui-icon layui-anim layui-anim-rotate layui-anim-loop"></i> 转换中...');
                    $.ajax({
                        type: "POST",
                        dataType: "json",
                        url: "{:url('push')}",
                        data: {change:'init',tablename:data.tablename,id:site_id},
                        success: function(res) {
                            layer.close(index);
                            if (res.code == 1) {
                                layer.open({
                                    type: 1,
                                    shade: false,
                                    title: '翻译推送状态',
                                    content: $('#change_div'),
                                    btn: ['关闭'],
                                    success:function(){
                                        $('#change_div').parents('div.layui-layer-page').find('div.layui-layer-btn>a').addClass('layui-btn-disabled').html('<i class="layui-icon layui-icon-loading layui-icon layui-anim layui-anim-rotate layui-anim-loop"></i> <span style="color:#aaa;">正在推送中，请勿关闭窗口</span>');
                                    },
                                    end: function(){
                                        $('#change_html').html('');
                                        $('#change_div').hide();
                                        table.reload('currentTable');
                                    }
                                });
                                $('#change_html').html('<p>'+res.msg+'</p>');
                                start_change(obj,that,1);
                            } else {
                                that.attr('disabled', false).removeClass("layui-btn-disabled").html('重新推送');
                                layer.msg(res.msg, { icon: 2 });
                            }
                        },
                        error: function(xhr, textstatus, thrown) {

                        }
                    });
                    return false;
                    layer.close(index);
                });
            }
        });
        //监听工具条
        table.on('toolbar(currentTable)', function(obj){
            var data = obj.data;
            if(obj.event==='pushAll'){
                //一键推送
                layer.confirm('操作将覆盖原有推送信息，是否继续操作？', function(index){
                    layer.close(index);
                    return false;
                });
            }else if(obj.event==='refresh'){
                //刷新
                table.reload('currentTable');
            }
        });
        function start_change(obj,that,page) {
            var data = obj.data;
            $.ajax({
                type: "POST",
                dataType: "json",
                url: "{:url('push')}",
                data: {change:'start',tablename:data.tablename,id:site_id,page:page},
                success: function(res) {
                    if (res.code == 1) {
                        if (res.data.code == 1) {
                            start_change(obj,that,page+1);
                            $('#change_html').append('<p>'+res.data.msg+'</p>');
                        } else if (res.data.code == 2) {
                            $('#change_html').append('<p>'+res.data.msg+'</p>');
                            $('#change_html').append('<p>'+res.msg+'</p>');
                            that.attr('disabled', false).removeClass("layui-btn-disabled").html('开始转换');
                            $('#change_div').parents('div.layui-layer-page').find('div.layui-layer-btn>a').removeClass('layui-btn-disabled').html('<i class="layui-icon layui-icon-close"></i> 关闭');
                            layer.msg(res.msg, { icon: 1 });
                        } else {

                        }
                    } else {
                        layer.msg(res.msg, { icon: 2 });
                        that.attr('disabled', false).removeClass("layui-btn-disabled").html('重新转换');
                        $('#change_div').parents('div.layui-layer-page').find('div.layui-layer-btn>a').removeClass('layui-btn-disabled').html('<i class="layui-icon layui-icon-close"></i> 关闭');
                    }
                },
                error: function(xhr, textstatus, thrown) {

                }
            });
        }

    });
</script>
{/block}

